<template>
  <page-header-wrapper :title="false">
    <a-card title="报废损失详情">
      <!-- <template #extra> <a-button type="primary" @click="handleEdit(detail)">编辑账户</a-button></template> -->
      <a-descriptions bordered title="" size="small">
        <a-descriptions-item label="操作人" :span="3"> {{ detail.operName }} </a-descriptions-item>
        <a-descriptions-item label="日期" :span="3">{{ detail.operDate }} </a-descriptions-item>
        <a-descriptions-item label="店面" :span="3"> {{ detail.storeName }} </a-descriptions-item>
        <a-descriptions-item label="数量" :span="3"> {{ detail.count }} </a-descriptions-item>
        <a-descriptions-item label="状态" :span="3">
          <template>
            <a-tag color="#82af6f" v-if="detail?.checkStatus == 1">已审核</a-tag>
            <a-tag color="#D15B47" v-else>未审核</a-tag>
          </template>
        </a-descriptions-item>
        <a-descriptions-item label="审核人" :span="3" v-if="detail?.checkStatus == 1"> {{ detail.checkerName }} </a-descriptions-item>
        <a-descriptions-item label="备注" :span="3"> {{ detail.memo }} </a-descriptions-item>
        <a-descriptions-item label="操作" :span="3">
          <div style="display: flex">
            <a-button type="danger" size="small" style="margin: 0px 5px" @click="setCX()" v-if="detail?.checkStatus == 0">撤销订单</a-button>
            <a-button
              type="success"
              size="small"
              style="background: #87b87f; margin: 0px 5px; color: #fff"
              @click="setSH()"
              v-if="detail?.checkStatus == 0"
            >审核</a-button
            >
            <a-button type="danger" size="small" style="margin: 0px 5px" @click="setFSH()" v-else>反审核</a-button>
          </div>
        </a-descriptions-item>
      </a-descriptions>
    </a-card>

    <a-card title="报废损失记录详情" style="margin-top: 24px">
      <template #extra>
        <a-tooltip>
          <template #title> 数据导出</template>
          <a-button type="primary" icon="database" @click="handleExpert(queryParam)"></a-button>
        </a-tooltip>
      </template>
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="8">
              <a-form-item label="筛选">
                <a-input v-model="queryParam.keyword" placeholder="请输入筛选" />
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 8) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="handleSearch(queryParam)" :loading="recordLoading">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-table ref="table" size="default" rowKey="key" :columns="columns" :data-source="loadData">
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="action" slot-scope="text, record">
          <template>
            <a-tag color="#428bca" @click="handleDetail(record)">进入</a-tag>
            <a-tag color="#D15B47" @click="handleDelete(record)">删除</a-tag>
          </template>
        </span>
      </a-table>
    </a-card>
   
  </page-header-wrapper>
</template>

<script>
import { getLossesUsingGET,revokeLosDetailUsingPOST,getLosDetailListUsingPOST,lossesCheckPurUsingPOST,deleteLosDetailUsingPOST,lossesUnCheckPurUsingPOST,exportLosDetailUsingPOST1 } from '@/api/api/kucunguanli.ts'
export default {
  name: 'LoseRecordDetail',
  components: {  },
  data() {
    return {
      mdl: null,
      visible: false,
      confirmLoading: false,
      turnMdl: null,
      turnVisible: false,
      turnConfirmLoading: false,
      queryParam: {
        pageNo: 1,
        pageSize: 10,
        queryCondition: '',
        status: '',
      },
      pagination: {},
      advanced: false,
      detail: {},
      recordLoading: false,
      columns: [
        {
          title: '商品名',
          dataIndex: 'goodsName',
        },
        {
          title: '串码',
          dataIndex: 'phoneCode1',
        },
        {
          title: '串码2',
          dataIndex: 'phoneCode2',
        },
        {
          title: '成本(元)',
          dataIndex: 'commCost',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
        },
      ],
      loadData: [],
    }
  },
  mounted() {
    this.getDetail()
    this.handleSearch(this.queryParam)
  },
  methods: {
    handleExpert(params){
      exportLosDetailUsingPOST1({...params, orderId: this.$route.params.id }).then((res) => {
          const blob = new Blob([res], {
            type: 'application/vnd.ms-excel;charset=utf-8',
          })
          const filename = '报损记录详情列表' + '.xlsx'
          // 将blob对象转为一个URL
          var blobURL = window.URL.createObjectURL(blob)
          // 创建一个a标签
          var tempLink = document.createElement('a')
          // 隐藏a标签
          tempLink.style.display = 'none'
          // 设置a标签的href属性为blob对象转化的URL
          tempLink.href = blobURL
          // 给a标签添加下载属性
          tempLink.setAttribute('download', filename)
          if (typeof tempLink.download === 'undefined') {
            tempLink.setAttribute('target', '_blank')
          }
          // 将a标签添加到body当中
          document.body.appendChild(tempLink)
          // 启动下载
          tempLink.click()
          // 下载完毕删除a标签
          document.body.removeChild(tempLink)
          window.URL.revokeObjectURL(blobURL)
          this.$message.success('导出成功~')
        })  
    },
    handleDelete(record){
      const that = this
      that.$confirm({
        title: '删除',
        content: '你确定要删除这个商品吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          deleteLosDetailUsingPOST({ id: record?.id }).then(() => {
          that.handleSearch(that.queryParam)
          that.$message.success('操作成功')
        })
        .catch((error) => {
          that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
          // this.loadData = this.loadData
        },
      })
    },
    setCX() {
      const that = this
      that.$confirm({
        title: '撤销订单',
        content: '你确定要撤销该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          revokeLosDetailUsingPOST({ id: that.detail.id })
            .then(() => {
              that.$router.back()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setSH() {
      const that = this
      that.$confirm({
        title: '审核订单',
        content: '你确定要审核通过该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          lossesCheckPurUsingPOST({ id: that.$route.params.id })
            .then(() => {
              that.getDetail()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    setFSH() {
      const that = this
      that.$confirm({
        title: '反审核',
        content: '你确定要反审核该订单吗？',
        okText: '确认',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          lossesUnCheckPurUsingPOST({ id:that.$route.params.id })
            .then(() => {
              that.getDetail()
              that.$message.success('操作成功')
            })
            .catch((error) => {
              that.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
            })
        },
      })
    },
    getDetail() {
      getLossesUsingGET({ id: this.$route.params.id })
        .then((res) => {
          this.detail = res.data
        })
        .catch(() => {
          this.$message.error('获取详情失败,请联系管理员')
        })
    },
    handleSearch(params) {
      this.recordLoading = true
      getLosDetailListUsingPOST({ ...params, orderId: this.$route.params.id })
        .then((res) => {
          this.loadData = res.data.list
          this.recordLoading = false
          this.recordPagination = { ...this.recordPagination, total: Number(res.data.total) }
        })
        .catch((error) => {
          this.recordLoading = false
          this.$message.error(((error.response || {}).data || {}).msg || '请求出现错误')
        })
    },
    handleRecordTableChange(pagination, filters, sorter) {
      const pager = { ...this.pagination }
      pager.current = pagination.current
 pager.pageSize = pagination.pageSize
      this.recordPagination = pager
      this.handleRecordSearch({
        sortField: sorter.field,
        sortOrder: sorter.order,
        ...filters,
        pageNo: pagination.current,
        pageSize: pagination.pageSize,
        queryCondition: '',
        status: '',
      })
    },
    handleDetail(record) {
      this.$router.push({ path: `/goods/name/${record.inventoryId}` })
    },
  },
}
</script>
